package com.weishuang.monitor.leader.config.security.handler;

import cn.hutool.json.JSONUtil;
import com.weishuang.monitor.leader.commons.entity.Result;
import com.weishuang.monitor.leader.commons.enums.RCODE;
import com.weishuang.monitor.leader.commons.utils.ResultUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomeLogoutSuccessHandler implements LogoutSuccessHandler {

    /**
     * 退出登录处理器
     * @param httpServletRequest
     * @param httpServletResponse
     * @param authentication
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        Result success = ResultUtils.success(RCODE.LOGOUT_SUCCESS);
        httpServletResponse.setStatus(200);
        httpServletResponse.setHeader("Content-type","application/json;charset=UTF-8");
        httpServletResponse.getWriter().write(JSONUtil.toJsonStr(success));
    }
}
